<?php

/**
 * cliente actions.
 *
 * @package    tvpv
 * @subpackage cliente
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class clienteActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->cliente_list = ClientePeer::doSelect(new Criteria());
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ClienteForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new ClienteForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($cliente = ClientePeer::retrieveByPk($request->getParameter('id')), sprintf('Object cliente does not exist (%s).', $request->getParameter('id')));
    $this->form = new ClienteForm($cliente);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($cliente = ClientePeer::retrieveByPk($request->getParameter('id')), sprintf('Object cliente does not exist (%s).', $request->getParameter('id')));
    $this->form = new ClienteForm($cliente);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($cliente = ClientePeer::retrieveByPk($request->getParameter('id')), sprintf('Object cliente does not exist (%s).', $request->getParameter('id')));
    $cliente->delete();

    $this->redirect('cliente/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
    	$usuario = new sfGuardUser();
    	$usuario->setUsername($form->getValue('usuario'));
    	$usuario->setPassword($form->getValue('password'));
    	$usuario->save();
    	/*$c = new Criteria();
    	$c->add(sfGuardUserPeer::ID,$usuario->getUsername(),Criteria::LIKE);
    	$utmp = sfGuardUserPeer::doSelect($c);*/
    	
    	$tmp = new ClienteForm();
    	$valores = $request->getParameter($tmp->getName());
      $valores['user_id'] = $usuario->getId();
    	$tmp->bind($valores, $request->getFiles($tmp->getName()));
    	if($tmp->isValid())
        $cliente = $tmp->save();
      $suscripcion = new Suscripcion();
      $suscripcion->setEmail($valores['email']);
      $suscripcion->save();
      $this->getUser()->setFlash('registrado','Registrado Exitosamente');
      $this->redirect("main/index");
    }
  }
  
  public function executeRegistrado(sfWebRequest $request)
  {
  	$this->cliente = ClientePeer::retrieveByPK($request->getParameter('id'));
  }
}
